package ph.com.gs3.formalistics.view.custom.fields;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.global.constants.GlobalConstants;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("ViewConstructor")
public class DateTimePickerField extends LinearLayout {

	public static final String TAG = DateTimePickerField.class.getSimpleName();

	private String id;

	private TextView tvLabel;
	private EditText etDateTimeText;
	private Button bSelectDateTime;

	private Context context;
	private PickerType pickerType;

	private DateTimePickerListener listener;

	public static enum PickerType {
		DATE, TIME, DATETIME
	}

	public DateTimePickerField(Context context, PickerType pickerType) {
		super(context);

		this.context = context;
		this.pickerType = pickerType;

		initialize();
	}

	private void initialize() {
		LayoutInflater.from(context).inflate(R.layout.field_date_time_picker, this, true);

		tvLabel = (TextView) findViewById(R.id.FldDTR_tvLabel);
		etDateTimeText = (EditText) findViewById(R.id.FldDTR_etDateText);
		bSelectDateTime = (Button) findViewById(R.id.FldDTR_bSelectDateTime);

		tvLabel.setTypeface(GlobalConstants.getApplicationInstance().getGlobalTypeface());
		etDateTimeText.setTypeface(GlobalConstants.getApplicationInstance().getGlobalTypeface());
		bSelectDateTime.setTypeface(GlobalConstants.getApplicationInstance().getGlobalTypeface());

		etDateTimeText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showPickerView();
			}

		});

		bSelectDateTime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showPickerView();
			}
		});

		FLLogger.d(TAG, "Picker type: " + pickerType.toString());

		if (pickerType == PickerType.DATETIME || pickerType == PickerType.TIME) {
			bSelectDateTime.setText("Select");
		}

		// TODO for testing only, remove later
		bSelectDateTime.setVisibility(View.GONE);

	}

	private void showPickerView() {

		if (listener != null) {
			listener.onOpenPickerViewCommand(this, pickerType, getValue());
		}

	}

	// =====================================================================
	// {{ Getters & Setters

	public void setId(String id) {
		this.setTag(id + "_container");
		etDateTimeText.setTag(id);
		this.id = id;
	}

	public String getFieldId() {
		return id;
	}

	@Override
	public void setEnabled(boolean enabled) {

		etDateTimeText.setEnabled(enabled);
		etDateTimeText.setClickable(enabled);

		// if (enabled) {
		// bSelectDateTime.setVisibility(View.VISIBLE);
		// } else {
		// bSelectDateTime.setVisibility(View.GONE);
		// }

	}

	public String getLabel() {
		return tvLabel.getText().toString();
	}

	public void setLabel(String label) {
		tvLabel.setText(label);
	}

	public String getValue() {
		return etDateTimeText.getText().toString();
	}

	public void setValue(String value) {
		etDateTimeText.setText(value);
	}

	public void setPlaceHolder(String placeholder) {

		String hint = "";

		if (placeholder == null || placeholder.isEmpty()) {
			switch (pickerType) {
				case DATE:
					hint = "MMM/dd/yyy";
					break;
				case TIME:
					hint = "HH:mm (AM/PM)";
					break;
				case DATETIME:
					hint = "MMM/dd/yyy HH:mm (AM/PM)";
					break;

			}
		} else {
			hint = placeholder;
		}

		etDateTimeText.setHint(hint);
	}

	public DateTimePickerListener getListener() {
		return listener;
	}

	public void setListener(DateTimePickerListener listener) {
		this.listener = listener;
	}

	// }}

	// =====================================================================
	// {{ Interface(s)

	public static interface DateTimePickerListener {

		public void onOpenPickerViewCommand(DateTimePickerField source, PickerType pickerType,
		        String currentFieldValue);

	}

	// }}

}
